<?php

/**
 * 
 * @author Nguyen Van Nhieu <nhieunv@evolable.asia>
 * Mar 27, 2013
 * File: comment.php
 *
 **/ 
class Comment extends AppModel {
	var $useTable = 'comments';
	var $limit = 3;
	var $order = array('Comment.created' => 'DESC');
	const OWNER_TYPE = 'owner';
	const NORMAL_TYPE = 'normal';
	const JOINED_TYPE = 'joined';

	/**
	 * 
	 * @param array $order
	 * @return data
	 */
	function getPagingOption($order, $limit) {
		if(isset($limit) && $limit > 0){
			$options['limit'] = $this->limit;
		}else{
			$options['limit'] = $this->limit;
		}
		$options['order'] = $this->_order($order);
		$options['fields'] = $this->_fields(true);
		$options['joins'] = array(
			array(
				'table' => 'profiles',
				'alias' => 'Profile',
				'type' => 'inner',
				'conditions' => array(
					$this->alias. '.user_id = Profile.user_id',
					'Profile.del_flg = 0'
				),
			),
		);
		$options['conditions'] = array(
			$this->alias.'.del_flg = 0'
		);
		return $options;
	}
	
	/**
	 * 
	 * @param array $commentIdArr
	 * @param int $courseId
	 * @return data
	 */
	function getReplyByCommentIdArray($commentIdArr, $courseId, $limit, $order) {
		if(is_array($commentIdArr)){
			$commentIds = implode(',', $commentIdArr);
			if(isset($limit) && $limit > 0) {
				$options['limit'] = $limit;
			}
			$options['order'] = $this->_order($order);
			$options['fields'] = $this->_fields(true);
			$options['conditions'] = array(
						$this->alias. '.del_flg = 0',
						$this->alias. '.comment_id in (' . $commentIds . ')',
						$this->alias. '.course_id' => $courseId
					);
			$options['joins'] = array(
			array(
				'table' => 'profiles',
				'alias' => 'Profile',
				'type' => 'inner',
				'conditions' => array(
					$this->alias. '.course_id' => $courseId,
					$this->alias. '.user_id = Profile.user_id',
					'Profile.del_flg' => 0
				)
			),
		);
			return $this->find('all', $options);
		}else{
			return array();
		}
	}
	
	/**
	 * 
	 * @param int $commentId
	 * @param int $limit
	 * @return type
	 */
	function getReplyByCommnentId($commentId, $limit, $order) {
		$options['order'] = $this->_order($order);
		if(isset($limit) && $limit > 0){
			$options['limit'] = $limit;
		}
		$options['fields'] = $this->_fields(true);
		$options['conditions'] = array(
					$this->alias. '.del_flg = 0',
					$this->alias. '.comment_id' => $commentId
				);
		$options['joins'] = array(
			array(
				'table' => 'profiles',
				'alias' => 'Profile',
				'type' => 'inner',
				'conditions' => array(
					$this->alias. '.comment_id' => $commentId,
					$this->alias. '.user_id = Profile.user_id',
					'Profile.del_flg' => 0
				)
			),
		);
		return $this->find('all', $options);
	}
	
	/**
	 * 
	 * @param int $commentId
	 * @param int $courseId
	 * @param int $limit
	 * @return type
	 */
	function getReplyByCommentIdAndCourseId($commentId, $courseId, $limit, $order) {
		$options['order'] = $this->_order($order);
		if(isset($limit) && $limit > 0){
			$options['limit'] = $limit;
		}
		$options['fields'] = $this->_fields(true);
		$options['conditions'] = array(
			$this->alias. '.course_id' => $courseId,
			$this->alias. '.del_flg' => 0,
			$this->alias. '.comment_id' => $commentId,
		);
		$options['joins'] = array(
			array(
				'table' => 'profiles',
				'alias' => 'Profile',
				'type' => 'inner',
				'conditions' => array(
					$this->alias. '.course_id' => $courseId,
					$this->alias. '.user_id = Profile.user_id',
					'Profile.del_flg' => 0
				)
			),
		);
		return $this->find('all', $options);
	}
	
	/**
	 * 
	 * @param int $commentId
	 * @param int $limit
	 * @return type
	 */
	function getReplyByCommentId($commentId, $limit, $order) {
		$options['order'] = $this->_order($order);
		if(isset($limit) && $limit > 0){
			$options['limit'] = $limit;
		}
		$options['fields'] = $this->_fields(true);
		$options['conditions'] = array(
			$this->alias. '.del_flg' => 0,
			$this->alias. '.comment_id' => $commentId,
		);
		$options['joins'] = array(
			array(
				'table' => 'profiles',
				'alias' => 'Profile',
				'type' => 'inner',
				'conditions' => array(
					$this->alias. '.comment_id' => $commentId,
					$this->alias. '.user_id = Profile.user_id',
					'Profile.del_flg' => 0
				)
			),
		);
		return $this->find('all', $options);
	}
	
	/**
	 * 
	 * @param int $courseId
	 * @param int $limit
	 * @param array $order
	 * @return type
	 */
	function getCommentsByCourseId($courseId, $limit, $order) {
		
		return $this->getReplyByCommentIdAndCourseId(0, $courseId, $limit, $order);
	}


	function getCommentById($fields, $id){
		if(isset($fields) && is_array($fields)){
			$options['fields'] = $fields;
		}else{
			$options['fields'] = $this->_fields(true);
		}
		$options['joins'] = array(
			array(
				'table' => 'profiles',
				'alias' => 'Profile',
				'type' => 'inner',
				'conditions' => array(
					$this->alias. '.user_id = Profile.user_id',
					'Profile.del_flg' => 0
				)
			),
		);
		$options['conditions'] = array(
			$this->alias. '.id' => $id
		);
		return $this->find('first', $options);
		
	}
	
	function deleteReplyById($commentId, $userId, &$parrentId) {
		$dataSource = $this->getDataSource();
		$dataSource->begin($this);
		$comment = $this->find('first', array('conditions' => array(
				$this->alias. '.id' => $commentId,
				$this->alias. '.user_id' => $userId
			),
		));
		if($comment){
			$parrentId = $comment['Comment']['comment_id'];
			$comment[$this->alias]['del_flg'] = 1;
			if($this->save($comment)){
				$queryString = " Update ". $this->useTable . " set total_reply = total_reply - 1 where id = '" . $parrentId . "'";
				if($this->query($queryString)){
					$dataSource->commit($this);
					return 1;
				}else{
					$dataSource->rollback($this);
					return -1;
				}
			}else{
				$dataSource->rollback($this);
				return -1;
			}
			
		}else{
			return 0;
		}
	}
	
	/**
	 * 
	 * @param int $commentId --> id
	 * @param int $userId
	 * @return int
	 */
	function deleteCommentById($commentId, $userId){
		$dataSource = $this->getDataSource();
		$dataSource->begin($this);
		$comment = $this->find('first', array('conditions' => array(
				$this->alias. '.id' => $commentId,
				$this->alias. '.user_id' => $userId
			),
		));
		if($comment){
			$comment[$this->alias]['del_flg'] = 1;
			if($this->save($comment)){
				$queryString = " Update ". $this->useTable . " set del_flg = 1 where comment_id = '" . $commentId . "'";
				if($this->query($queryString)){
					$dataSource->commit($this);
					return 1;
				}else{
					$dataSource->rollback($this);
					return -1;
				}
			}else{
				$dataSource->rollback($this);
				return -1;
			}
			
		}else{
			return 0;
		}
	}
	function saveComment($data) {
		$commentId = $data['comment_id'];
		$dataSource = $this->getDataSource();
		$dataSource->begin($this);
		if($this->save($data)){
			$querString = " Update comments set total_reply = total_reply +1 where id = '" . $commentId . "'";
			if($this->query($querString)){
				$dataSource->commit($this);
				return true;
			}else{
				$dataSource->rollback($this);
				return false;
			}
		}
		return false;
	}
	
	function isOwner($commentId){
		return $this->find('first', array(
			'conditions' => array(
				$this->alias. '.id' => $commentId,
				$this->alias. '.del_flg' => 0
			)
		));
	}
	function _order($order) {
		if(isset($order) && is_array($order)){
			return $order;
		}else{
			return $this->order;
		}
	}
	
	/**
	 * 
	 * @param boolean $isJoinProfile
	 * @return type
	 */
	function _fields($isJoinProfile){
		if($isJoinProfile == true){
			return array($this->alias. '.*', 'Profile.user_id', 'Profile.first_name', 'Profile.last_name', 'Profile.icon_image');
		}else{
			return array($this->alias. '.*');
		}
	}
	
	/**
	 * 
	 * @param int $userId
	 * @return data array
	 */
	function getAllCommentOfUser($userId) {
		$options['conditions'] = array(
			$this->alias. '.user_id' => $userId,
			$this->alias. '.del_flg' => 0
		);
		return $this->find('all', $options);
	}
}